import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_transaction.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_purchased_item.dart';

abstract class UpdateGooglePlayPurchasedSubscriptionTransactionFromApiOne {
  GooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayPurchasedSubscriptionTransaction purchasedSubscriptionTransaction,
    required GooglePlayApiPurchasedSubscription updatedApiPurchasedSubscription,
  });
}

class UpdateGooglePlayPurchasedSubscriptionTransactionFromApiOneImpl
    implements UpdateGooglePlayPurchasedSubscriptionTransactionFromApiOne {
  const UpdateGooglePlayPurchasedSubscriptionTransactionFromApiOneImpl();

  @override
  GooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayPurchasedSubscriptionTransaction purchasedSubscriptionTransaction,
    required GooglePlayApiPurchasedSubscription updatedApiPurchasedSubscription,
  }) {
    if (updatedApiPurchasedSubscription.orderId == purchasedSubscriptionTransaction.orderId) {
      return purchasedSubscriptionTransaction.copyWith(
        expirationTime: () => updatedApiPurchasedSubscription.expirationTime,
      );
    } else {
      return purchasedSubscriptionTransaction;
    }
  }
}
